<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\Fangs;
use App\Models\Fangattr;
use Elasticsearch\ClientBuilder;

class FangsController extends BaseController
{
    //房源推荐
    public function fangrecommendlist(Request $request){
          $field = [
              'id',
              'fang_name',
              'fang_pic',
              'fang_shi',
              'fang_ting'
          ];
          //查询5条数据
          $data = Fangs::where('is_recommend',1)->orderBy('id','desc')->select($field)->limit(5)->get();
          return $data;
    }
    
    //房源属性列表
    public function attrlist(Request $request){
        //需要获取的房源属性字段
         $field_name = $request->get('field_name');
         $pid = Fangattr::where('field_name',$field_name)->value('id');
         $data = Fangattr::where('pid',$pid)->orderBy('id','asc')->limit(4)->select(['id','name','icon'])->get();
         return $data;
    }
    
    
    //使用es来进行搜索
    public function esfang(Request $request){
          //根据关键字查询
          $kw = $request->get('kw');
          if(empty($kw)){
              return ['status' => 1000,'msg' => '没有数据',data => []];
          }
          //调用es类来查询
          $client = ClientBuilder::create()->setHosts(config('es.host'))->build();
          //查询参数
          $params = [
              'index' => 'fang_tt',
              'type' => '_doc',
              'body' => [
                  'query' => [
                      'match' => [
                          'fang_desn' => [
                              'query' => $kw
                          ]
                      ]
                  ]
              ]
          ];
          //执行查询
          $results = $client->search($params);
          $total = $results['hits']['total']['value'];
          //判断是否查询到结果
          if($total > 0){//代表有结果
                //房源id
              $ids = array_column($results['hits']['hits'], '_id');
              $field = [
                  'id',
                  'fang_name',
                  'fang_build_area',
                  'fang_rent',
                  'fang_shi',
                  'fang_ting',
                  'fang_pic'
              ];
              $data = Fangs::select($field)->whereIn('id',$ids)->paginate(10);
              return $data;
          }
          return ['status' => 1009,'msg' => '没有存数据','data' => []];
    }
    
    
    
    //获取房源列表
    public function fanglist(Request $request){
        //查询现在状态是正常的房源
        $field = [
            'id',
            'fang_name',
            'fang_build_area',
            'fang_rent',
            'fang_shi',
            'fang_ting',
            'fang_pic'
        ];
        $builder = Fangs::select($field);
        if($request->get('fieldname')){
            $builder->where($request->get('fieldname'),$request->get('fieldvalue'));
        }
        $list = $builder->where('fang_status',1)->orderBy('id','desc')->paginate($this->pagesize);
        return $list;
    }
    
    
    //获取房源详情信息
    //在控制器中使用模型
    public function fangDetail(Fangs $fang){
        //得到房东
        $fang->owner;
        $fang->owner->pic = config('url.url').$fang->owner->pic;
        //房屋配置
        $configArr = explode(',',$fang->fang_config);
        $fang->config = Fangattr::whereIn('id',$configArr)->get(['id','name','icon']);
        //房源朝向
        $fang->direction = Fangattr::where('id',$fang->fang_direction)->value('name');
        //房子图片
        $fang->pic_list_show = array_map(function($item){
            return config('url.url').$item;
        }, explode('#',$fang->pic_list));
        return $fang;        
    }
    
    
    
    
    
    
    
    
    
}
